package com.epfresh.supplier.adapter;

import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.epfresh.api.global.BaseApplication;
import com.epfresh.api.global.DataManager;
import com.epfresh.api.universalimageloader.core.ImageLoader;
import com.epfresh.api.utils.CashierInputFilter;
import com.epfresh.api.utils.FormatUtil;
import com.epfresh.supplier.R;
import com.epfresh.supplier.entity.Goods;

import java.math.BigDecimal;

import multitype.ItemViewBinder;
import multitype.OnRecyclerViewItemClickListener;


public class PriceSureViewBinder extends ItemViewBinder<Goods, PriceSureViewBinder.ViewHolder> implements View.OnClickListener {

    OnRecyclerViewItemClickListener onRecyclerViewItemClickListener;


    public boolean[] showFlag = {true,true,false};

    public OnRecyclerViewItemClickListener getOnRecyclerViewItemClickListener() {
        return onRecyclerViewItemClickListener;
    }

    public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener onRecyclerViewItemClickListener) {
        this.onRecyclerViewItemClickListener = onRecyclerViewItemClickListener;
    }

    //更新某个位置的数据
    public void notifyItem(int position) {
        if (position > -1 && position < getAdapter().getItemCount())
            getAdapter().notifyItemChanged(position);
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(
            @NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.item_price_sure, parent, false);
        return new ViewHolder(view);
    }


    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull Goods post) {

        int position = getPosition(holder);
        holder.ivMinus.setTag(R.id.item_key_position, position);
        holder.ivMinus.setTag(R.id.item_key_status, post);
        holder.ivMinus.setOnClickListener(this);
        holder.ivAdd.setTag(R.id.item_key_position, position);
        holder.ivAdd.setTag(R.id.item_key_status, post);
        holder.ivAdd.setOnClickListener(this);
        holder.etPrice.setTag(position);
        holder.etPrice.setTag(R.id.item_key_position, position);
        holder.etPrice.setTag(R.id.item_key_status, post);
        holder.etPrice.addTextChangedListener(new AttTextWatcher(holder));
   //     holder.rl_item.setVisibility(View.GONE);
        holder.setData(post);
/*        if(showFlag[0]&&showFlag[1]&&showFlag[2]){
            holder.rl_item.setVisibility(View.VISIBLE);
            return;
        }else if(showFlag[0]){
            if(post.getPriceNow()!=null&&post.getPriceNow()>post.getPrice())
                holder.rl_item.setVisibility(View.VISIBLE);
        }else if(showFlag[1]){
            if(post.getPriceNow()!=null&&post.getPriceNow()<post.getPrice())
                holder.rl_item.setVisibility(View.VISIBLE);
        }else if(showFlag[2]){
            if(post.getPriceNow()==null||post.getPriceNow()==post.getPrice())
                holder.rl_item.setVisibility(View.VISIBLE);
        }*/
    }

    @Override
    public void onClick(View v) {

        Object tag = v.getTag(R.id.item_key_position);
        if (tag != null && tag instanceof Integer){
            int position = (int) tag;
            Object o = v.getTag(R.id.item_key_status);
          if (v.getId() == R.id.tv_update_info || v.getId() == R.id.tv_update) {
          } else if (v.getId() == R.id.tv_group) {
          } else if (onRecyclerViewItemClickListener != null && o instanceof Goods) {
              onRecyclerViewItemClickListener.onItemClick(v, position, o);
          }
        }
    }
    private class AttTextWatcher implements TextWatcher {

        ViewHolder holder = null;
        public AttTextWatcher(ViewHolder holder){
            this.holder = holder;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            Goods goods = (Goods)holder.etPrice.getTag(R.id.item_key_status);
            if (TextUtils.isEmpty(s.toString())) {
                return;
            }
            try {
                Double.parseDouble(s.toString());//损失
            } catch (Exception ex) {
                return;
            }
            goods.setPriceNow(new BigDecimal(Double.parseDouble(s.toString())).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue());
            DataManager.getInstance().putPriceGoods(goods.getId(),goods);
            if(!goods.getPrice().equals(goods.getPriceNow())){
                holder.etPrice.setTextColor(Color.RED);
            }else{
                DataManager.getInstance().removePriceGoods(goods.getId());
                holder.etPrice.setTextColor(0xff333333);
            }
        }

    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView ivAdd;
        ImageView ivMinus;
        TextView tvTitle;
        EditText etPrice;
        TextView tvUnit;
        TextView tv_sale_price_title;
        View rl_item;

        ViewHolder(@NonNull View itemView) {
            super(itemView);
            ivAdd = (ImageView) itemView.findViewById(R.id.iv_add);
            ivMinus = (ImageView) itemView.findViewById(R.id.iv_minus);
            tvTitle = (TextView) itemView.findViewById(R.id.tv_title);
            tvUnit = (TextView) itemView.findViewById(R.id.tv_unit);
            etPrice = (EditText) itemView.findViewById(R.id.et_price);
            etPrice.setFilters(new InputFilter[]{new CashierInputFilter()});
            tv_sale_price_title = (TextView) itemView.findViewById(R.id.tv_sale_price_title);
            rl_item = itemView.findViewById(R.id.rl_item);
        }
        void setData(Goods post) {
            tvTitle.setText(post.getTitle());
            tvUnit.setText(post.getPriceUnitStr());
            tv_sale_price_title.setText("从"+post.getPrice()+"元调整至");
            if(DataManager.getInstance().getPriceGoods(post.getId())!=null&&DataManager.getInstance().getPriceGoods(post.getId()).getPriceNow()!=post.getPrice()){
                etPrice.setText(FormatUtil.subZeroAndDot(""+DataManager.getInstance().getPriceGoods(post.getId()).getPriceNow()));
                etPrice.setTextColor(Color.RED);
            }else{
                etPrice.setText(FormatUtil.subZeroAndDot(""+post.getPrice()));
                etPrice.setTextColor(0xff333333);
            }
        }
    }
}
